/**
 * Created by root on 17-8-21.
 */

var schedule = require('node-schedule');
var http = require('../utils/http');
var logger = require('../config/log4js');
var cheerio = require('cheerio');
var ipListService = require('../service/ipListService');

var jobs = [];

function getData(Url) {
    var rule = new schedule.RecurrenceRule();
    rule.minute = [3, 18, 33, 48];
    var job = schedule.scheduleJob(rule, function () {
        logger.access.info('66ipHttpProxyTask定时任务: ' + new Date());

        http.httpGetByURL(Url, function (result) {
            if (result.statusCode === 200) {
                var $ = cheerio.load(result.body);
                $('#main tr').each(function () {
                    var ipList = $(this).children();
                    if ($(ipList[0]).text() !== 'ip' && $(ipList[0]).text() !== ' ') {
                        console.log($(ipList[0]).text() + ':' + $(ipList[1]).text());
                        ipListService.addIP({
                            ip: $(ipList[0]).text(),
                            port: $(ipList[1]).text(),
                            type: 0
                        }, function (result) {
                        });
                    }
                });
            } else {
                logger.error.info(result);
            }
        });
    });
    jobs.push(job);
}

module.exports = {
    start : function () {
        getData('http://www.66ip.cn/');
        getData('http://www.66ip.cn/areaindex_1/1.html');
        getData('http://www.66ip.cn/areaindex_2/1.html');
        getData('http://www.66ip.cn/areaindex_3/1.html');
        getData('http://www.66ip.cn/areaindex_4/1.html');
        getData('http://www.66ip.cn/areaindex_5/1.html');
        getData('http://www.66ip.cn/areaindex_6/1.html');
        getData('http://www.66ip.cn/areaindex_7/1.html');
        getData('http://www.66ip.cn/areaindex_8/1.html');
        getData('http://www.66ip.cn/areaindex_9/1.html');
        getData('http://www.66ip.cn/areaindex_10/1.html');
        getData('http://www.66ip.cn/areaindex_11/1.html');
        getData('http://www.66ip.cn/areaindex_12/1.html');
        getData('http://www.66ip.cn/areaindex_13/1.html');
        getData('http://www.66ip.cn/areaindex_14/1.html');
        getData('http://www.66ip.cn/areaindex_15/1.html');
        getData('http://www.66ip.cn/areaindex_16/1.html');
        getData('http://www.66ip.cn/areaindex_17/1.html');
        getData('http://www.66ip.cn/areaindex_18/1.html');
        getData('http://www.66ip.cn/areaindex_19/1.html');
        getData('http://www.66ip.cn/areaindex_20/1.html');
        getData('http://www.66ip.cn/areaindex_21/1.html');
        getData('http://www.66ip.cn/areaindex_22/1.html');
        getData('http://www.66ip.cn/areaindex_23/1.html');
        getData('http://www.66ip.cn/areaindex_24/1.html');
        getData('http://www.66ip.cn/areaindex_25/1.html');
        getData('http://www.66ip.cn/areaindex_26/1.html');
        getData('http://www.66ip.cn/areaindex_27/1.html');
        getData('http://www.66ip.cn/areaindex_28/1.html');
        getData('http://www.66ip.cn/areaindex_29/1.html');
        getData('http://www.66ip.cn/areaindex_30/1.html');
        getData('http://www.66ip.cn/areaindex_31/1.html');
        getData('http://www.66ip.cn/areaindex_32/1.html');
        getData('http://www.66ip.cn/areaindex_33/1.html');
        getData('http://www.66ip.cn/areaindex_34/1.html');
        logger.access.info('66ipHttpProxyTask Start!');
    },
    stop : function () {
        for (var i = 0; i < jobs.length; i++) {
            jobs[i].cancel();
        }
    }
}
